import * as echarts from 'echarts';

export const base_pie= (caverageSalaries,PercentRequirements,lowerPercentNum,div_name)=> {

    const myChart = echarts.init(document.getElementById(div_name));

    let option;
    let title = '';
    // 提取并处理数据
    const pieData = PercentRequirements.map(item => {
        const key = Object.keys(item)[0]; // 获取对象中的键
        const value = item[key]; // 获取对应值
        return { value, name: key }; // 返回 ECharts 需要的数据格式
    });

    const caverage_text=Object.keys(caverageSalaries)
    // console.log(caverage_text[0])
    if (caverage_text[0] === "lower25Percent") {
        title = '后25%的工资';

    }
    else if (caverage_text[0] === "middle50Percent")
    {
        title = '中间50%的工资';
    }
    else if (caverage_text[0] === "upper25Percent")
    {
        title = '前25%的工资';
    }

    const value = caverageSalaries[caverage_text]
    const valueRounded = parseFloat(value.toFixed(0));


    option = {
        title: {
            text: title+':'+ valueRounded+"K（年薪）",
            subtext: "此工资段有"+lowerPercentNum+"个岗位",
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '所需技能',
                type: 'pie',
                radius: '50%',
                grid: { containLabel: true },
                data: pieData, // 使用处理后的数据
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    myChart.setOption(option);
    myChart.resize();
    window.addEventListener('resize', () => myChart.resize);
}
